package com.lry.web.cartoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.web.cartoon.entity.Cartoon;
import com.lry.web.cartoon.entity.CartoonUpdate;
import com.lry.web.cartoon.mapper.CartoonUpdateMapper;
import com.lry.web.cartoon.service.CartoonUpdateService;
import com.lry.web.cartoon.entity.Cartoon;
import com.lry.web.cartoon.entity.CartoonUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartoonUpdateServiceImpl implements CartoonUpdateService {
    @Autowired
    CartoonUpdateMapper cartoonUpdateMapper;


    public int setCartoonUpdate(CartoonUpdate cartoonUpdate) {
        return cartoonUpdateMapper.insert(cartoonUpdate);
    }


    public List<CartoonUpdate> getCartoonUpdates() {
        return cartoonUpdateMapper.selectList(null);
    }


    public int setCartoonUpdateUp(CartoonUpdate cartoonUpdate) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cartoon_id",cartoonUpdate.getCartoonId());
        return cartoonUpdateMapper.update(cartoonUpdate,wrapper);
    }


    public int deleteByCartoonId(String cartoonId) {
        List<String> listCId = Arrays.asList(cartoonId.split(","));
        int i=0;
        for(String id:listCId){
            i = cartoonUpdateMapper.deleteByCartoonId(id);
        }
        return i;
    }


    public int deleteByCVId(String cVId) {
        return cartoonUpdateMapper.deleteByCVId(cVId);
    }

    @Override
    public Map<String, CartoonUpdate> selectByCartoon(List<Cartoon> cartoons) {
        Map<String, CartoonUpdate> map = new HashMap<>();
        for(Cartoon v: cartoons){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("cartoon_id",v.getCartoonId());
            map.put(v.getCartoonId(),cartoonUpdateMapper.selectOne(wrapper));
        }
        return map;

    }

    @Override
    public int updateDeleted(String cartoonId) {
        return cartoonUpdateMapper.updateDeleted(cartoonId);
    }

    @Override
    public int deleteByCId(String id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cartoon_id",id);
        return cartoonUpdateMapper.delete(wrapper);
    }

}
